package cz.muni.fi.pa165.pokemon.service.impl;

import cz.muni.fi.pa165.pokemon.dao.PokemonDAO;
import cz.muni.fi.pa165.pokemon.dto.PokemonTO;
import cz.muni.fi.pa165.pokemon.entity.Pokemon;
import cz.muni.fi.pa165.pokemon.service.PokemonService;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Dusan Svancara
 */
@Service
public class PokemonServiceImpl implements PokemonService {
    
    @Autowired
    private PokemonDAO pokemonDAO;
    
    @Autowired
    private DozerBeanMapper mapper;

    @Transactional
    @Override
    public void create(PokemonTO pokemon) {
        if (pokemon == null) {
            throw new IllegalArgumentException("pokemon is null");
        }
        if (pokemon.getId() != null) {
            throw new IllegalArgumentException("pokemon.id is not null");
        }
        
        Pokemon es = mapper.map(pokemon, Pokemon.class);
        pokemonDAO.create(es);
        pokemon.setId(es.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public PokemonTO get(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        
        return mapper.map(pokemonDAO.get(id), PokemonTO.class);
    }

    @Transactional
    @Override
    public void update(PokemonTO pokemon) {
        if (pokemon == null) {
            throw new IllegalArgumentException("pokemon is null");
        }
        if (pokemon.getId() == null) {
            throw new IllegalArgumentException("pokemon.id is null");
        }
        
        Pokemon es = mapper.map(pokemon, Pokemon.class);
        pokemonDAO.update(es);
    }

    @Transactional
    @Override
    public void remove(PokemonTO pokemon) {
        if (pokemon == null) {
            throw new IllegalArgumentException("pokemon is null");
        }
        if (pokemon.getId() == null) {
            throw new IllegalArgumentException("pokemon.id is null");
        }
        
        Pokemon es = mapper.map(pokemon, Pokemon.class);
        pokemonDAO.remove(es);
    }

    @Transactional(readOnly = true)
    @Override
    public List<PokemonTO> findAll() {
        List<Pokemon> result = pokemonDAO.findAll();
        List<PokemonTO> ret = new ArrayList<PokemonTO>();
        for (Pokemon es : result) {
            ret.add(mapper.map(es, PokemonTO.class));
        }
        
        return ret;
    }

    @Transactional(readOnly = true)
    @Override
    public List<PokemonTO> findByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("name is null");
        }
        
        List<Pokemon> result = pokemonDAO.findByName(name);
        List<PokemonTO> ret = new ArrayList<PokemonTO>();
        for (Pokemon es : result) {
            ret.add(mapper.map(es, PokemonTO.class));
        }
        
        return ret;
    }
    
}
